<?xml version="1.0" encoding="UTF-8"?>
<ui:composition template="/pages/mastertemplate/MasterPage.xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui">
	<ui:define name="content">
		<p:panel header="#{msg.hoadon_title}">
			<h:form>
				<div>
					<p:selectOneRadio id="options"
						value="#{HoaDonBean.loai}">
						<f:selectItem for="options"
							itemLabel="#{msg.hoaDon}" itemValue="0" />
						<f:selectItem for="options"
							itemLabel="#{msg.chiTietHoaDon}" itemValue="1" />
							<p:ajax action="#{HoaDonBean.setLoai}" event="valueChange" />
					</p:selectOneRadio>
				</div>
			</h:form>
			<br/>
			<h:form>
				<p:fileUpload widgetVar="fileuplaod_wgt"
					fileUploadListener="#{HoaDonBean.handleFileUpload}"
					mode="advanced" dragDropSupport="false" update=":hoaDonForm"
					sizeLimit="100000" fileLimit="1"
					allowTypes="/(\.|\/)(#{DataConfigBean.fileExtensionPattern})$/" />
			</h:form>
			<h:form>
				<p:commandButton style="width:150px;float: left;margin-top:10px;"
					value="#{msg.Button_export}"
					actionListener="#{HoaDonBean.downloadFile}" ajax="false">
					<p:fileDownload value="#{HoaDonBean.file}" />
				</p:commandButton>
			</h:form>

			<h:form id="hoaDonForm">
				<p:growl id="growforFile" autoupdate="true"
					life="#{DataConfigBean.growlShow}" />
				<p:dataTable id="dataTable" var="hoadon"
					value="#{HoaDonBean.hoaDonModel}" paginator="true"
					rows="10"
					paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
					rowsPerPageTemplate="10,20,50,100"
					style="width: 100%; margin: 45px auto;">

					<f:facet name="header">
						<h:outputText value="#{msg.danhsachHoaDon}"
							styleClass="tableHeader" />
						<div style="margin: -4px -11px 5px; float: right;">
							<p:commandButton style="height: 25px"
								update=":addHoaDonForm:dialog_1 :hoaDonForm:growforFile" value="+ #{msg.themmoi}"
								rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
								action="#{HoaDonBean.notifyAdd}"
								oncomplete="if(args.opSuccess) PF('hoaDonDialog').show();">
								<p:resetInput target=":addHoaDonForm:dialog_1" />
							</p:commandButton>
						</div>

					</f:facet>
					<p:column headerText="#{msg.soHoaDon}">#{hoadon.soHoaDon}  </p:column>
					<p:column headerText="#{msg.doiTac}">#{hoadon.doiTac}  </p:column>
					<p:column headerText="#{msg.ngayHoaDon}">
						<h:outputText value="#{hoadon.ngayHoaDon}">
									<f:convertDateTime pattern="dd-MM-yyyy"
										value="#{hoadon.ngayHoaDon}" />
						</h:outputText>  
					</p:column>
					<p:column headerText="#{msg.ngayHachToan}">
						<h:outputText value="#{hoadon.ngayHachToan}">
									<f:convertDateTime pattern="dd-MM-yyyy"
										value="#{hoadon.ngayHachToan}" />
						</h:outputText>  
					</p:column>
					<p:column headerText="#{msg.hinhThucThanhToan}">#{hoadon.hinhThucThanhToan.value}  </p:column>
					<p:column headerText="#{msg.tienTe}">#{hoadon.tienTe}  </p:column>
					<p:column headerText="#{msg.tyGia}">#{hoadon.tyGia==0?'':hoadon.tyGia}  </p:column>
					<p:column headerText="#{msg.donHang}">#{hoadon.donHang}  </p:column>
					<p:column headerText="#{msg.tongTien}">#{hoadon.tongTien==0?'':hoadon.tongTien}  </p:column>
					<p:column headerText="#{msg.tongTienUSD}">#{hoadon.tongTien_USD==0?'':hoadon.tongTien_USD}  </p:column>
					<p:column headerText="#{msg.loaiHoaDon}">#{hoadon.loaiHoaDon.value}  </p:column>
					<p:column headerText="#{msg.trangThai}">#{hoadon.trangThai.value}  </p:column>
					<p:column style="width:60px;text-align: center;"
						headerText="#{msg.table_actions}">
						<p:commandButton styleClass="smallButton"
							icon="ui-icon ui-icon-pencil" update=":addHoaDonForm:dialog_1"
							rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
							action="#{HoaDonBean.notifyEdit(hoadon)}"
							oncomplete="PF('hoaDonDialog').show();">
							<p:resetInput target=":addHoaDonForm:dialog_1" />
						</p:commandButton>
						<p:commandButton style="margin-left: 5px" styleClass="smallButton"
							icon="ui-icon ui-icon-trash"
							rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
							oncomplete="PF('confirmRemove').show();">
							<f:setPropertyActionListener value="#{hoadon}"
								target="#{HoaDonBean.selectedHoaDon}" />
						</p:commandButton>
					</p:column>

				</p:dataTable>

				<p:dialog id="confirmRemove" header="#{msg.confirm}"
					widgetVar="confirmRemove" resizable="false">
					<table style="width: 200px; height: 50px">
						<tr>
							<td><h:outputText value="#{msg.confirm_text}" /></td>
						</tr>
						<tr>
							<td style="float: right;"><p:commandButton value="#{msg.okButton}"
									actionListener="#{HoaDonBean.removeHoaDon}"
									update=":hoaDonForm" oncomplete="PF('confirmRemove').hide();" /></td>
						</tr>

					</table>
				</p:dialog>
			</h:form>
			<h:form id="addHoaDonForm">
				<p:dialog id="dialog_1" header="Thêm mới/ Sửa" modal="true"
					widgetVar="hoaDonDialog" resizable="false">

					<p:panel id="panel_1">
						<table style="margin-left: 150px">
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										for="soHoaDon" value="#{msg.soHoaDon}"/></td>
								<td><p:inputText styleClass="inputText" required="true"
										requiredMessage="#{msg.soHoaDon} #{msg.valuerequired}"
										id="soHoaDon" value="#{HoaDonBean.hoaDon.soHoaDon}" readonly="#{HoaDonBean.checkEdit}">
									</p:inputText></td>
								<td style="width: 30px"></td>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.loaiHoaDon}"/></td>
								<td><p:selectOneMenu styleClass="inputText"
										value="#{HoaDonBean.hoaDon.loaiHoaDon}">
										<f:selectItems value="#{HoaDonBean.loaiHoaDon}"
											var="selectItem" itemLabel="#{selectItem}"
											itemValue="#{selectItem}" />
									</p:selectOneMenu></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.ngayHoaDon}"/></td>
								<td><p:calendar style="min-width:140px"
										value="#{HoaDonBean.hoaDon.ngayHoaDon}"
										pattern="#{DataConfigBean.datepattern}" showButtonPanel="true">
									</p:calendar></td>
								<td style="width: 30px"></td>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.ngayHachToan}"/></td>
								<td><p:calendar style="min-width:140px"
										value="#{HoaDonBean.hoaDon.ngayHachToan}"
										pattern="#{DataConfigBean.datepattern}" showButtonPanel="true">
									</p:calendar></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.hinhThucThanhToan}"/></td>
								<td><p:selectOneMenu styleClass="inputText"
										value="#{HoaDonBean.hoaDon.hinhThucThanhToan}">
										<f:selectItems value="#{HoaDonBean.hinhThuc}"
											var="selectItem" itemLabel="#{selectItem}"
											itemValue="#{selectItem}" />
									</p:selectOneMenu></td>
								<td style="width: 30px"></td>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.tienTe}"/></td>
								<td><p:selectOneMenu styleClass="inputText"
										value="#{HoaDonBean.hoaDon.tienTe}">
										<f:selectItems value="#{HoaDonBean.tienTe}"
											var="selectItem" itemLabel="#{selectItem}"
											itemValue="#{selectItem}" />
									</p:selectOneMenu></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.doiTac}" for="doitac" /></td>
								<td><p:selectOneMenu styleClass="inputText" id="doitac" required="true"
								requiredMessage="#{msg.doiTac} #{msg.valuerequired}"
										value="#{HoaDonBean.hoaDon.doiTac}">
										<f:selectItem itemLabel="" itemValue="" />
										<f:selectItems value="#{HoaDonBean.listDT}"
											var="selectItem" itemLabel="#{selectItem.maDoiTac}"
											itemValue="#{selectItem.maDoiTac}" />
										<p:ajax event="change" update=":addHoaDonForm:donhang"
        									listener="#{HoaDonBean.listDonHang()}" />
									</p:selectOneMenu></td>
								<td style="width: 30px"></td>
								<td><p:outputLabel style="margin-left: 20px" for="donhang"
										value="#{msg.donHang}"/></td>
								<td><p:selectOneMenu id="donhang" styleClass="inputText" required="true"
								requiredMessage="#{msg.donHang} #{msg.valuerequired}"
										value="#{HoaDonBean.hoaDon.donHang}">
										<f:selectItem itemLabel="" itemValue="" />
										<f:selectItems value="#{HoaDonBean.listDH}"
											var="selectItem" itemLabel="#{selectItem.soChungTu}"
											itemValue="#{selectItem.soChungTu}" />
									</p:selectOneMenu></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.tyGia}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.tyGia} #{msg.invalid_number}"
										value="#{HoaDonBean.hoaDon.tyGia}">
									</p:inputText></td>
								<td style="width: 30px"></td>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.trangThai}"/></td>
								<td><p:selectOneMenu styleClass="inputText"
										value="#{HoaDonBean.hoaDon.trangThai}">
										<f:selectItems value="#{HoaDonBean.trangThai}"
											var="selectItem" itemLabel="#{selectItem}"
											itemValue="#{selectItem}" />
									</p:selectOneMenu></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.tongTien}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.tongTien} #{msg.invalid_number}"
										value="#{HoaDonBean.hoaDon.tongTien}">
									</p:inputText></td>
								<td style="width: 30px"></td>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.tongTienUSD}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.tongTienUSD} #{msg.invalid_number}"
										value="#{HoaDonBean.hoaDon.tongTien_USD}">
									</p:inputText></td>
							</tr>
						</table>
						<p:dataTable id="dataTableChiTiet" var="chitiet"
							value="#{HoaDonBean.chiTietModel}" paginator="true"	rows="10"
							paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
							rowsPerPageTemplate="10,20,50,100"
							style="width: 100%; margin: 45px auto;">
		
							<f:facet name="header">
								<h:outputText value="#{msg.chiTietHoaDon}"
									styleClass="tableHeader" />
								<div style="margin: -4px -11px 5px; float: right;">
									<p:commandButton style="height: 25px"
										update=":addChiTietForm :addHoaDonForm:panel_1 :hoaDonForm:growforFile" value="+ #{msg.themmoi}"
										rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
										actionListener="#{HoaDonBean.notifyAddChiTiet}"
										oncomplete="if(args.opSuccess) PF('chiTietDialog').show();">
									</p:commandButton>
								</div>
		
							</f:facet>
							<p:column headerText="#{msg.soHoaDon}">#{chitiet.hoaDon} </p:column>
							<p:column headerText="#{msg.mavthh}">#{chitiet.maVthh}  </p:column>
							<p:column headerText="#{msg.tenvthh}">#{chitiet.tenVthh}  </p:column>
							<p:column headerText="#{msg.soLuong}">#{chitiet.soLuong==0?'':chitiet.soLuong}  </p:column>
							<p:column headerText="#{msg.dongia}">#{chitiet.donGiaMua==0?'':chitiet.donGiaMua}  </p:column>
							<p:column headerText="#{msg.dongia_US}">#{chitiet.donGiaMua_USD==0?'':chitiet.donGiaMua_USD}  </p:column>
							<p:column headerText="#{msg.thanhtien}">#{chitiet.thanhTien==0?'':chitiet.thanhTien}  </p:column>
							<p:column headerText="#{msg.thanhtien_US}">#{chitiet.thanhTien_USD==0?'':chitiet.thanhTien_USD}  </p:column>
							<p:column headerText="#{msg.thueGiaTri}">#{chitiet.thueGTGT==0?'':chitiet.thueGTGT}  </p:column>
							<p:column style="width:60px;text-align: center;"
								headerText="#{msg.table_actions}">
								<p:commandButton style="margin-left: 5px" styleClass="smallButton"
									icon="ui-icon ui-icon-trash"
									rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
									actionListener="#{HoaDonBean.removeChiTiet(chitiet.maVthh)}"
									update=":addHoaDonForm:dataTableChiTiet">
								</p:commandButton>
							</p:column>
						</p:dataTable>
						<br/>
						<p:commandButton 
							value="#{msg.general_ok}" action="#{HoaDonBean.save}"
							update=":hoaDonForm:dataTable :hoaDonForm:growforFile :addHoaDonForm:panel_1"
							oncomplete="if(args.opSuccess)PF('hoaDonDialog').hide()"></p:commandButton>
					</p:panel>
				</p:dialog>
			</h:form>
			<h:form id="addChiTietForm">
				<p:dialog id="dialog_2" header="Thêm mới/ Sửa" modal="true"
					widgetVar="chiTietDialog" resizable="false">
					<p:panel id="panel_2">
						<table>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.mavthh}"/></td>
								<td><p:selectOneMenu styleClass="inputText"
										value="#{HoaDonBean.chiTiet.maVthh}">
										<f:selectItems value="#{HoaDonBean.listCtDH}"
											var="selectItem" itemLabel="#{selectItem.vatTuHangHoa}"
											itemValue="#{selectItem.vatTuHangHoa}" />
									</p:selectOneMenu>
								</td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.soLuong}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.soLuong} #{msg.invalid_number}"
										value="#{HoaDonBean.chiTiet.soLuong}">
									</p:inputText></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.dongia}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.dongia} #{msg.invalid_number}"
										value="#{HoaDonBean.chiTiet.donGiaMua}">
									</p:inputText></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.dongia_US}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.dongia_US} #{msg.invalid_number}"
										value="#{HoaDonBean.chiTiet.donGiaMua_USD}">
									</p:inputText></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.thueGiaTri}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.thueGiaTri} #{msg.invalid_number}"
										value="#{HoaDonBean.chiTiet.thueGTGT}">
									</p:inputText></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.thanhtien}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.thanhtien} #{msg.invalid_number}"
										value="#{HoaDonBean.chiTiet.thanhTien}">
									</p:inputText></td>
							</tr>
							<tr>
								<td><p:outputLabel style="margin-left: 20px"
										value="#{msg.thanhtien_US}"/></td>
								<td><p:inputText styleClass="inputText"
										converterMessage="#{msg.thanhtien_US} #{msg.invalid_number}"
										value="#{HoaDonBean.chiTiet.thanhTien_USD}">
									</p:inputText></td>
							</tr>
							<tr>
								<td></td>
								<td align="right"><p:commandButton value="#{msg.general_ok}"
										action="#{HoaDonBean.saveChiTiet}"
										update=":hoaDonForm:growforFile :addHoaDonForm:panel_1 :addChiTietForm:panel_2"
										oncomplete="if(args.opSuccess)PF('chiTietDialog').hide();"></p:commandButton></td>
							</tr>
						</table>
					</p:panel>
				</p:dialog>
			</h:form>
		</p:panel>
	</ui:define>
</ui:composition>
